using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ScannerLib.FrameGrabber;
using System.IO;

namespace ScannerLib.Visualizer
{
    public partial class HoughSnapshot : Form
    {
        private Capture m_boundCapture;
        private Bitmap m_boundBitmap;
        private IntPtr m_boundIntPtr;

        public HoughSnapshot()
        {
            InitializeComponent();
        }

        public Capture BoundCapture
        {
            get
            {
                return m_boundCapture;
            }

            set
            {
                m_boundCapture = value;
            }
        }

        public Bitmap BoundBitmap
        {
            get
            {
                return m_boundBitmap;
            }

            set
            {
                m_boundBitmap = value;
                m_visualizer.Bitmap = value;
            }
        }

        public IntPtr BoundIntPtr
        {
            get
            {
                return m_boundIntPtr;
            }

            set
            {
                m_boundIntPtr = value;
            }
        }

        private void m_btnFindGrid_Click(object sender, EventArgs e)
        {
            string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "__test.pgm");

            try
            {
                FrameWriter.WritePGM(path, m_boundCapture.Width, m_boundCapture.Height, m_boundCapture.Stride, 3, 0, 1, 2, m_boundIntPtr);
                KeyValuePair<LineHoughSettings,List<Point>> hough =  GridFinder.FindGrid("__test.pgm", m_edgeLevel.Value, m_houghLevel.Value);

                m_visualizer.HoughSettings = hough.Key;
                m_visualizer.HoughPoints = hough.Value;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }

        }

        private void m_btnViewEdges_Click(object sender, EventArgs e)
        {
            string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "__test.pgm");

            try
            {
                FrameWriter.WritePGM(path, m_boundCapture.Width, m_boundCapture.Height, m_boundCapture.Stride, 3, 0, 1, 2, m_boundIntPtr);
                m_visualizer.Bitmap = GridFinder.GetEdges(path, m_edgeLevel.Value);
                m_visualizer.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }

        private void m_btnRestoreOriginal_Click(object sender, EventArgs e)
        {
            m_visualizer.Bitmap = m_boundBitmap;
        }

        private void m_btnViewHough_Click(object sender, EventArgs e)
        {
            string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "__test.pgm");

            try
            {
                FrameWriter.WritePGM(path, m_boundCapture.Width, m_boundCapture.Height, m_boundCapture.Stride, 3, 0, 1, 2, m_boundIntPtr);
                m_visualizer.Bitmap = GridFinder.GetHough(path, m_edgeLevel.Value,m_houghLevel.Value);
                m_visualizer.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }


        
    }
}